﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.IO;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
namespace GroundStation.Models.Launcher
{
    // This class is used to connect the user's entity to tthe database. The user will log in using their call sign, which will be verified against the call signs in the database
    class User
    {
        private string _callSign, _bandwidth;
        private TcpClient _tcpClient;


        public User(string callSign, string bandwidth)
        {
            this._callSign = callSign;
            this._bandwidth = bandwidth;
            StreamWriter sw;
            _tcpClient = new TcpClient();
            _tcpClient.Connect(Properties.Settings.Default.UserIP, Properties.Settings.Default.ChatPort);
            sw = new StreamWriter(_tcpClient.GetStream());
            sw.WriteLine(this.CallSign);
            sw.Flush();
        }
        public TcpClient TcpClient
        {
            get
            {

                return _tcpClient;
            }
        }
        public static User GetUser(string callSign, string bandwidth)
        {
            using( MySqlConnection con= new MySqlConnection(Properties.Settings.Default.SDTTest))
            {
                con.Open();
                using (MySqlCommand command = new MySqlCommand("Select * from sdt.Users where CallSign= @callSign ",con)){
                    command.Parameters.AddWithValue("@callSign",callSign);
                    using (MySqlDataReader reader = command.ExecuteReader())
                    {
                        if (reader.HasRows == true)
                            return new User(callSign, bandwidth);
                        else
                            return null;
                    }

                }

            }

            return null;
        }
        public string CallSign
        {
            get
            {
                return _callSign;
            }
            set
            {
                _callSign = value;
            }
        }
        public string Bandwidth
        {
            get
            {
                return _bandwidth;
            }
            set
            {
                _bandwidth = value;
            }


        }

    }
}
